package com.zrrd.yunchmall.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.Menu;
import com.zrrd.yunchmall.user.entity.Resource;
import com.zrrd.yunchmall.user.entity.Role;
import com.zrrd.yunchmall.user.service.IMenuService;
import com.zrrd.yunchmall.user.service.IResourceService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "权限服务—菜单模块")
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private RedisTemplate redisTemplate;




    @ApiOperation("查询菜单树")
    @GetMapping("/treeList")
    public ResponseResult treeList() {
        List<Menu> menuList = menuService.getTreeList();
        return new ResponseResult(200,"查询成功", menuList);
    }

    @ApiOperation("查询菜单列表")
    @GetMapping("/list/{id}")
    public ResponseResult list(
            @ApiParam(required = true, defaultValue = "10") @RequestParam int pageNum,
            @ApiParam(required = true, defaultValue = "1") @RequestParam int pageSize,
            @PathVariable("id") Integer id
            ){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if (id != 0){
            queryWrapper.eq("parent_id",id);
        }
        //分页操作
        IPage<Menu> pageInfo = menuService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return new ResponseResult<>(200,"查询成功",pageInfo);
    }


    @ApiOperation("添加一个菜单")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Menu menu){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", menu.getName());
        if (menuService.count(queryWrapper) > 0) {
            return new ResponseResult(205,"注册失败，用户名已存在");
        }
        queryWrapper.eq("title", menu.getTitle());
        queryWrapper.eq("parent_id",menu.getParentId());
        queryWrapper.eq("name",menu.getName());
        queryWrapper.eq("icon",menu.getIcon());
        queryWrapper.eq("hidden",menu.getHidden());
        queryWrapper.eq("sort",menu.getSort());
        //设置创建的时间
        menu.setCreateTime(LocalDateTime.now());
        //在数据库中保存管理员对象
        menuService.save(menu);
        return new ResponseResult(200,"注册成功");
    }

    @ApiOperation("修改菜单信息")
    @PostMapping("/update/{id}")
    public ResponseResult update(@RequestBody Menu menu,@PathVariable("id") long id)
    {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        menuService.update(menu,updateWrapper);
        redisTemplate.delete("MenuTree");
        return new ResponseResult(200,"修改成功");
    }
    @ApiOperation("菜单是否显示")
    @PostMapping("/updateHidden/{id}")
    public ResponseResult updatePublishStatus(@ApiParam("显示状态") @RequestParam int hidden,
                                              @ApiParam("菜单的id") @PathVariable("id") long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("hidden", hidden);
        menuService.update(updateWrapper);
        return new ResponseResult<>(200, "修改显示状态成功");
    }
    @ApiOperation("删除菜单")
    @PostMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") long id) {
        menuService.removeById(id);
        redisTemplate.delete("MenuTree");
        return new ResponseResult(200,"删除成功");
    }

    @ApiOperation("查看")
    @GetMapping("/{id}")
    public ResponseResult watch(@PathVariable("id") long id){
        List<Menu> menuList = menuService.getmenulist(id);
        return new ResponseResult(200,"查询成功",menuList);
    }

}
